﻿(function($){
$.__DNJ__ = function(options){
    var settings = 
    {	  
        target:null,        
        success: null,
        beforeCallBack : null,
        afterCallBack : null,
        errorCallBack : null,
        successCallBack : null,
        importantFields: ['__EVENTVALIDATION', '__VIEWSTATE', '__EVENTARGUMENT', '__EVENTTARGET']
    };	  	  
    //ensure unique instance    
    var dnjObj = this;
    window.__dnjObj = window.__dnjObj || dnjObj;
    //get request counter back or initialize it
    dnjObj.jsoncount = dnjObj.jsoncount || 1;

    if(options) { $.extend(settings, options);}
    var DNJTag = '__DNJ__';
    panelSelector = '.' + DNJTag;
    this.clickTarget = null;          
           
    function SetupDNJ()
    {
        $('body').click(function(e){    
            this.clickTarget = e.originalTarget; 
        }); 
            
        $('form '+panelSelector+' input[type=submit]').live('click', function()
        {         
            return ESubmit($('form:first'), $(this));        
        });  
        
        if (theForm)
        {                
            var oldSubmit = theForm.submit;
            theForm.submit = function() {                        
                sender = ($('#__EVENTTARGET').val() != '') ? $('#'+$('#__EVENTTARGET').val()) : null;                        
                if (sender == null) sender = $(this.clickTarget);
                
                if (sender != null && $(panelSelector + ' #' + sender.attr('id')).length > 0)
                     return ESubmit($(theForm), sender);                        
                
                
                if (document.all) //IE ? (IE seems not to understand call() method
                    oldSubmit(theForm, arguments);
                else //other
                    oldSubmit.call(theForm, arguments);
            }
        }          
    }
    
    function ESubmit(dotNetForm, sender)
        {
            if ($.isFunction(settings.beforeCallBack)) settings.beforeCallBack(sender);
            
            var dotNetData=dotNetForm.serialize() + '&'+DNJTag+'=1';
            if (sender != null)
                dotNetData += '&' + sender.attr('name') + '=' + sender.attr('value')
            
            
            
            $.ajax({
                type: "POST",
                data: dotNetData,
                success: function(msg) {                                        
                    this.clickTarget = null; 
                    
				    $res = jQuery("<div/>").append(msg.replace(/<head(.|\s)*?\/head>/g, ""));
                    
				    //first update .Net hidden fields (__EVENTTARGET __EVENTVALIDATION ...etc)
                    $.each(settings.importantFields, function(i, n){
                       $vobj = $res.find('#'+n);
                       if ($vobj.length > 0)
                       {
                           $('#'+n).val($vobj.val());
                       }
                    });
				    
				    dotNetForm.find(panelSelector).each(function(i, n){				        
				        $(this).html($res.find('#'+$(this).attr('id')).html());				        
				    });
                    if ($.isFunction(settings.successCallBack)) settings.successCallBack(sender, msg);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    this.clickTarget = null; 
                    msg = $("<div/>").append(XMLHttpRequest.responseText.replace(/<title(.|\s)*?\/title>/g, "").replace(/<style(.|\s)*?\/style>/g, ""));
                    if ($.isFunction(settings.errorCallBack)) settings.errorCallBack(msg.html());                    
                },                  
                complete : function(XMLHttpRequest, textStatus) {
                    if ($.isFunction(settings.afterCallBack)) settings.afterCallBack(sender);
                }
            });           
            
            return false;
      }

    function rpcSuccessCB(data){
        if (typeof(settings.success) == 'function')
            settings.success(data);
        if (settings.target != null)
        {
            trg = $(settings.target);
            if (typeof(trg.html) == 'function')
                trg.html(data.result);
            if (typeof(trg.val) == 'function')
                trg.val(data.result);
               
            settings.target = null;
        }                                   
    };


      
      SetupDNJ();
   return jQuery;};
})(jQuery);

$(document).ready(function(){
$.DNJ2();
});

